package cn.dlc.com.config;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.platform.commons.util.CollectionUtils;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 邓立川
 * @date 2022/1/15 16:18
 * @desc 当前类的描述信息
 * 整合elasticsearch
 */
@Configuration
public class ElasticSearchConfig {

    @Value("${dlc.elasticsearch.hostlist}")
    private String hostList;

    @Bean
    public RestHighLevelClient restHighLevelClient(){
        if(StringUtils.isNotBlank(hostList)){
            System.out.println("hostList: " + hostList);

            String[] hostListArr = hostList.split(",");
            // 放到数组里面
            HttpHost[] hosts = new HttpHost[hostListArr.length];
            for (int i = 0; i < hostListArr.length; i++) {
                String[] item = hostListArr[i].split(":");
                hosts[i] = new HttpHost(item[0],Integer.parseInt(item[1]),"http");
            }

            return new RestHighLevelClient(RestClient.builder(hosts));

        }
        return null;
    }
}
